package niuKe.HW;

import java.util.*;

public class HJ14 {
    public static void main(String[] args) {
        int n = 9;
        String[] str = {"cap", "to", "cat", "card", "two", "too", "up", "boat", "boot"};
//        method_1(str);
//        method_2(str);
        method_3(str);
    }

    private static void method_3(String[] str) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                int i = 0;
                while(i < s1.length() && i < s2.length()) {
                    if (s1.charAt(i) != s2.charAt(i)) {
                        return (s1.charAt(i) > s2.charAt(i)) ? 1 : -1;
                    }
                    i++;
                }
                if (s1.length() == s2.length()) {
                    return 0;
                } else {
                    return (s1.length() > s2.length()) ? 1 : -1;
                }
            }
        });
        System.out.println(list);
    }

    /**
     * 优先级队列
     * @param str
     */
    private static void method_2(String[] str) {
        PriorityQueue<String> pq = new PriorityQueue<>();
        for (int i = 0; i < str.length; i++) {
            pq.offer(str[i]);
        }
        for (int i = 0; i < str.length; i++) {
            str[i] = pq.poll();
        }
        System.out.println(Arrays.toString(str));
    }

    /**
     * 使用API: Arrays.sort()
     * @param str
     */
    private static void method_1(String[] str) {
        Arrays.sort(str);
        System.out.println(Arrays.toString(str));
    }
}
